package com.mac.macjetpackdemo.view

import android.view.View

abstract class MeetViewAdapter {
    protected var mData = mutableListOf<ContainerBean>()
    private var mObserver = mutableListOf<AdapterDataObserver>()

    fun setData(data: MutableList<ContainerBean>) {
        mData = data
        notifyDataSetChanged()
    }

    fun add(bean: ContainerBean) {
        mData.add(bean)
        update()
    }

    fun remove(bean: ContainerBean) {
        mData.remove(bean)
        update()
    }

    fun update(bean: ContainerBean) {
        val indexOf = mData.indexOf(bean)
        mData.removeAt(indexOf)
        mData.add(indexOf, bean)
    }

    fun update() {
        notifyDataSetChanged()
    }

    fun registerAdapterDataObserver(observer: AdapterDataObserver) {
        mObserver.add(observer)
    }

    fun notifyDataSetChanged() {
        synchronized(mObserver) {
            mObserver.forEach {
                it.onChanged(mData)
            }
        }
    }

    abstract fun getView(position: Int): View

    interface AdapterDataObserver {
        fun onChanged(data: MutableList<ContainerBean>)
    }
}
